import TableData from "@/components/public/el-table-pagination"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import CustomDialog from "@/components/PhysicalQualityPage/reboundStrengthManage/reboundDetectionTask/index.vue";

import formJs from "./regexDetail.js"; //新增弹框配置
import {
    getConcreteCheckTaskPageList, //列表
    copyConcreteCheckTask, // 复制
    submitConcreteCheckTask, //提交
    delConcreteCheckTaskTask, //删除
    getUserCompanyTreeList

} from "@/api/request.js";
export default {
    name: "reboundDetectionTaskText",
    data() {
        return {
            name: "reboundDetectionTaskText",
            radio: "待办",
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            showName: "未选择公司",
            checkData: [], //树状图默认选中id
            btnObj: [], //按钮
            loading: false,
            getList: {
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                modelType: "0", // 0待办 1已办 2查询
                userId: "", // 查询待办或已办时传 列表页查询不传
                fCompanyid: "",
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
            },
            queryCriteriaData: {}, //分页
            table_config: formJs.table_config, //列表
            dataFid: null, //列表详情id
            tableData: [],
            isShow: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            currentRow: {}, //列表详情
            dialogTitle: "",
            userInfo: {}, //登录用户

        };
    },
    components: {
        TableLyout,
        CustomDialog, //详情
        TableData, //主页面列表
    },
    created() { },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        console.log("userInfo", this.userInfo);


        this.yj_searchForm(this.getList);
        this.btnRole(this.name);
        this.getTree();
        this.table_config.operation.cols = [
            {
                prop: "fStatus",
                type: "primary",
                handleRow: this.handleRow,
            },
        ];
        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待检测") {
                color = "red !important";
            } else if (rowData == "检测中") {
                color = "orange !important";
            } else if (rowData == "已完成") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };

    },
    methods: {
        // 弹框
        refreshList() { //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
            this.isShow1 = false;
        },
        getTree() {
            this.treeText.companyId = this.userInfo.companyId;
            this.treeText.userId = this.userInfo.userId;
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("yjSmart-reboundStrengthManage_treeCheck")
                            ).fShortname;
                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }
                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },


        // 待办/已办
        modelType(e) {
            this.currentRow = "";
            this.dataFid = "";
            if (e == "待办") {
                this.getList.modelType = "0";
            } else {
                this.getList.modelType = "1";
            }
            this.yj_searchForm(this.getList);
        },

        // 列表操作按钮
        handleRow() { },

        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                modelType: data.modelType, // 0待办 1已办 2查询
                userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
                fCompanyid: data.fCompanyid,
                page: data.page, // 当前页码
                pageSize: data.pageSize, // 每页显示条数
            }
            this.loading = true;
            getConcreteCheckTaskPageList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        that.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表", res.msg);
                        this.loading = false;
                    }
                })
                .catch(() => {
                    this.loading = false;
                    console.log("获取列表数据接口报错");
                });
        },


        // 导航栏-右侧按钮
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            this.statusNum = status;
            this.yj_edit(status)
        },
        // 导航栏---右侧按钮
        // 新增/编辑
        yj_edit(num) {
            if (this.userInfo.companyNature == "总监办" || this.userInfo.companyNature == "施工单位" || this.userInfo.companyNature == "中心试验室") {
                if (num == 1) { // 新增
                    this.dialogTitle = "新增";
                    this.editText();
                } else if (num == 2) { //复制
                    if (this.dataFid != null) {
                        this.yj_copy(this.currentRow);
                    } else {
                        this.$message.warning("请先选择需要复制的数据");
                    }
                } else if (num == 3) { //编辑
                    this.dialogTitle = "编辑";
                    if (this.dataFid != null) {
                        if (this.currentRow.fStatus == "编辑中" || this.currentRow.fStatus == "被撤回") {
                            this.editText();
                        } else {
                            this.$message.warning("当前数据不可编辑");
                        }
                    } else {
                        this.$message.warning("请先选择需要编辑的数据");
                    }

                } else if (num == 4) { //提交
                    if (this.dataFid != null) {
                        if (this.currentRow.fStatus == "编辑中" || this.currentRow.fStatus == "被撤回") {
                            if (this.currentRow.fProjecttypename != "" && this.currentRow.fProjecttypename != null) {
                                this.yj_submit(this.currentRow);
                            } else {
                                this.$message.warning("请先填写工程部位！");
                            }
                        }
                    } else {
                        this.$message.warning("请先选择需要提交的数据");
                    }
                } else if (num == 5) { //删除
                    this.yj_delete(this.currentRow);
                }
            }
            else {
                this.$message.warning("该用户无法操作");
            }
        },
        // 编辑
        editText() {
            this.isShow = true;
        },
        // 复制

        yj_copy(data) {
            var obj = {
                fId: data.fId,
                userId: this.userInfo.userId, //当前登录人id
                userName: this.userInfo.realName //当前登录人姓名
            }
            this.loading = true;
            copyConcreteCheckTask(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.yj_searchForm(this.getList)
                        this.$message.success("复制成功");
                    } else {
                        this.loading = false;

                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },
        // 提交
        yj_submit() {
            if (this.currentRow != null) {
                if (this.dataFid != "" && this.dataFid != null) {
                    this.$confirm("是否继续提交?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.isLock = true
                            this.loading = true;
                            submitConcreteCheckTask({
                                fId: this.dataFid,
                                userId: this.userInfo.userId, //当前登录人id
                                userName: this.userInfo.realName //当前登录人姓名
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.dataFid = null
                                        this.isLock = false
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else {
                                        this.isLock = false
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                        return false;
                                    }
                                })
                                .catch(() => {
                                    this.loading = false;
                                    console.log("提交失败");
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error("请先选择需要提交的数据");
                }
            }
        },

        // 删除
        yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.$confirm("您是否要永久删除此数据?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.loading = true;
                        this.isLock = true
                        delConcreteCheckTaskTask({
                            fId: this.dataFid,
                            // userId: this.userInfo.userId, //当前登陆用户ID
                            // userName: this.userInfo.realName, //当前登陆用户名称
                        })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.isLock = false
                                    this.loading = false;
                                    this.yj_searchForm(this.getList);
                                    this.$message.success("删除成功");
                                    this.dataFid = null

                                } else {
                                    this.isLock = false
                                    this.$message.error("删除失败", res.msg);
                                    this.loading = false;
                                }
                            })
                            .catch(() => {
                                console.log("删除失败");
                                this.loading = false;
                            });
                    })
                    .catch(() => {
                        this.$message("取消删除");
                    });
            } else {
                return this.$message.error("请先选择需要删除的数据");
            }
        },
        // 点击查看
        yj_viewForm(row, label) {


            // if (label == "报验单编号") {
            //     this.statusNum1 = 7;
            //     this.isShow1 = true;
            // }
            if (label == "工程部位") {
                this.dialogTitle = "查看";
                this.statusNum = 7;
                this.currentRow = row;
                this.isShow = true;
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
    },
};